<template>
  <div id="youshi">
    <div id="sticky">
      <div id="frames">
        <canvas id="zhuban" width="1920" height="1080" style="transform: translate(-20%)"></canvas>
        <div class="info" :class="{ show: index>15 }">
          <div class="tip">
            <div class="line"></div>
            <div class="text">1T内存大空间</div>
          </div>
          <div class="tip">
            <div class="line"></div>
            <div class="text">i5主板超强性能</div>
          </div>
        </div>
      </div>
      <div class="youshi">
        <div class="title" data-aos="fade-up">手感细腻 横式立式皆可摆放</div>
        <div class="sub" data-aos="fade-up" style="max-width: 999px">采用阳极氧化喷绘工艺，表面成色均匀，主机强韧不变形</div>

        <div class="blank" style="height: 50px"></div>
        <img class="scale" data-aos="fade-up" src="@/assets/images/197.png" alt />

        <div>
          <img class="xs-hidden" data-aos="fade-right" src="@/assets/images/ys11.png" alt />
          <img class="scale" data-aos="fade-up" src="@/assets/images/ys1.png" alt />
          <img class="xs-hidden" data-aos="fade-left" src="@/assets/images/ys12.png" alt />
        </div>

        <img class="scale" data-aos="fade-up" src="@/assets/images/ys2.png" alt />

        <div class="pics">
          <div class="col-center">
            <img data-aos="fade-right" src="@/assets/images/38.png" alt />
            <img data-aos="fade-right" src="@/assets/images/hs1.png" alt />
          </div>
          <div class="col-center">
            <img data-aos="fade-left" class="heng" src="@/assets/images/hs11.png" alt />
            <img data-aos="fade-left" src="@/assets/images/hs12.png" alt />
          </div>
        </div>
        <div class="pics">
          <img data-aos="fade-right" src="@/assets/images/hs2.png" alt />
          <img data-aos="fade-left" src="@/assets/images/hs22.png" alt />
        </div>

        <div class="title" data-aos="fade-up">接口齐备 轻松扩展</div>
        <div class="sub" data-aos="fade-up">人人云平衡于轻薄与实用之间， 配备多种扩展接口，即插即用，方便快捷</div>

        <img data-aos="fade-up" src="@/assets/images/jiekou.png" style="width: 80%;" alt />
        <img data-aos="fade-up" src="@/assets/images/jiekou2.png" class="jiekou" alt />
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
export default {
  mounted() {
    this.init();
  },
  data() {
    return {
      index: null
    };
  },
  methods: {
    init() {
      let that = this;
      $("#zhuban").width($(window).width());

      $(window).on("scroll", function() {
        let frames = document.getElementById("youshi");
        let stickyCt = document.getElementById("sticky");

        let scrolled = stickyCt.offsetTop / frames.offsetHeight;

        let frame = Math.ceil(scrolled * 55);
        changeFrame(frame);
      });

      const loader = new PxLoader();
      const images = [];

      for (let i = 0; i < 56; i++) {
        images[i] = loader.addImage(require(`../assets/frames/zb${i + 1}.png`));
      }

      loader.addCompletionListener(function() {
        let context = $("#zhuban")[0].getContext("2d");

        $("body").addClass("loaded");

        context.drawImage(images[0], 0, 0, 1920, 1080);
      });

      loader.start();

      function changeFrame(frame) {
        let index = frame - 1;
        that.index = index;
        if (index < 0) index = 0;
        if (index > 55) index = 55;

        let context = $("#zhuban")[0].getContext("2d");
        context.clearRect(0, 0, 1920, 1080);
        context.drawImage(images[index], 0, 0, 1920, 1080);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
#youshi {
  height: 500vh;
  position: relative;
}
#sticky {
  position: sticky;
  top: 0;
}
#frames {
  position: relative;
}
.info {
  position: absolute;
  top: 50%;
  right: 10%;
  transform: translateY(-60%);
  transition: all 0.5s;
  opacity: 0;
  &.show {
    opacity: 1;
  }
  .tip {
    margin: 40px 0;
    display: flex;
    align-items: center;
  }
  .line {
    width: 50px;
    height: 2px;
    background: #13c2c2;
    margin-right: 8px;
    position: relative;
  }
  .line::before {
    content: "";
    width: 8px;
    height: 8px;
    background: #13c2c2;
    border-radius: 50%;
    top: 50%;
    left: 0;
    transform: translate(-50%, -50%);
    position: absolute;
  }
  .text {
    height: 16px;
    font-size: 12px;
    font-family: OPPOSans-M, OPPOSans;
    font-weight: normal;
    color: rgba(51, 51, 51, 1);
    line-height: 16px;
  }
}
.youshi {
  background-image: url("../assets/images/xnbg.png");
  background-size: 100%;
  background-repeat: no-repeat;
  padding-top: 30%;
  padding-bottom: 10%;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  .title {
    margin-top: 30px;
    height: 21px;
    font-size: 16px;
    font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
    font-weight: bold;
    color: rgba(255, 255, 255, 1);
    line-height: 21px;
    letter-spacing: 1px;
  }
  .sub {
    margin-top: 20px;
    max-width: 255px;
    text-align: center;
    height: 15px;
    font-size: 11px;
    font-family: MicrosoftYaHei;
    color: rgba(153, 153, 153, 1);
    line-height: 15px;
  }
  .pics {
    width: 50%;
    display: flex;
    justify-content: space-between;
  }
}
.jiekou {
  width: 60%;
}
.jiekou.aos-animate {
  transform: translate(-8%, -110%) !important;
}
@media screen and (max-width: 360px) {
  .xs-hidden {
    display: none;
  }
}
</style>